/**
 * WangSong 2009-10-4 重构.仅包含从数据库获取信息的代码
 */
package cn.biplam.back.services.database;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import cn.biplam.back.jda.Jda;
import cn.biplam.common.bean.sqlengine.Sql;
import cn.biplam.common.client.DbResource;
import cn.biplam.common.interfaces.DbResourceServer;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.KvMap;
import cn.biplam.common.wrapper.sqlresult.SqlResult;


/**
 * 数据源类的功能是负责存储数据源的参数
 * @author WangSong Feb 27, 2009 10:20:15 AM
 * @version 1.0
 */
public class DbResourceServerImpl extends UnicastRemoteObject implements DbResourceServer{

	/** */
	private static final long serialVersionUID = 8338818770453251320L;
	/**
	 * String sDefaultDbResourceName  	默认数据源名称
	 * boolean defaultDsOnly = true		只取默认数据源连接表示
	 * int iDsid						数据源id
	 * String sDsname					数据源名称
	 * String sDsdesc					数据源描述 
	 * String sDsaddress				数据源地址
	 * String sDssid					数据源sid
	 * String sUsername					数据库用户名
	 * String password					数据库密码
	 * String sDbtype               	数据库类型
	 * String sMaindb              		主数据库
	 * String sDbVersion          		数据库版本
	 */

	private Lock mLock=new ReentrantLock();
	private int iCount=0;


	/**
	 * 构造方法
	 * @throws RemoteException
	 */
	public DbResourceServerImpl() throws RemoteException{
		this.addCount();
	}

	/**
	 * 指定数据源中的数据源信息表获取数据源信息
	 * @param defaultDbResorceName		系统数据源名称
	 * @param dbResouceName				目标数据源名称			
	 * @throws RemoteException 
	 * @throws ErrorMessage 
	 */
	public KvMap<String,String> get(String defaultDbResorceName,String dbResouceName) throws RemoteException, ErrorMessage{
		KvMap<String,String> kv=null;
		try{
			addCount();
			SqlResult sr = queryDb(dbResouceName);
			kv=initFromDb(sr);
		}catch(ErrorMessage e){
			throw e;
		}catch(Exception e){
			throw new ErrorMessage(e,"Get DbResource from db failed!");
		}finally{
			reduceCount();
		}
		return kv;
	}
	
	private SqlResult queryDb(String dbResouceName) throws ErrorMessage{
		Jda mJda = new Jda(new DbResource());
		StringBuffer sql = new StringBuffer();
		sql.append("select ");
		sql.append(		"dsname as name,");
		sql.append(		"dsdesc as describ,");
		sql.append(		"dstype as dsType,");
		sql.append(		"db_type_id as dbType,");
		sql.append(		"ip,");
		sql.append(		"port,");
		sql.append(		"dssid as sid,");
		sql.append(		"main_db as mainDb,");
		sql.append(		"username ,");
		sql.append(		"password ");
		sql.append("from dbsource ");
		sql.append("where dsid='"+dbResouceName+"'");
		Sql sqlc = new Sql(sql.toString());
		return mJda.executeSelect(sqlc);
	}

	/**
	 * 得到的查询结果来实例化DbResource内部参数
	 * @param sr SqlResult
	 * @throws ErrorMessage 
	 */
	private KvMap<String,String> initFromDb(SqlResult sr)  throws RemoteException, ErrorMessage{
		KvMap<String,String> kv = new KvMap<String,String>();
		if(sr!=null && sr.size()>0){
			kv.put("name", sr.get(0,"name"));
			kv.put("desc", sr.get(0,"describ"));
			kv.put("dsType",sr.get(0,"dsType"));
			kv.put("dbType", sr.get(0,"dbtype"));
			kv.put("ip", sr.get(0,"ip"));
			kv.put("port", sr.get(0,"port"));
			kv.put("sid", sr.get(0,"sid"));
			kv.put("mainDb", sr.get(0,"mainDb"));
			kv.put("user", sr.get(0,"username"));
			kv.put("password", sr.get(0,"password"));
		}
		return kv;
	}

	public int getCount() throws RemoteException {
		return this.iCount;
	}

	private synchronized void addCount() {
		this.iCount++;
	}

	private synchronized void reduceCount() {
		this.iCount--;
	}

	public void lock() throws RemoteException {
		mLock.lock();
	}

	public boolean trylock() throws RemoteException {
		return mLock.tryLock();
	}

	public void unlock() {
		mLock.unlock();
	}

}
